package com.erlinyou.im.dao.repository.group;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.Query;

import com.erlinyou.base.dao.BaseRepository;
import com.erlinyou.im.model.group.Groupmembers;

public interface GroupmembersRepository extends BaseRepository<Groupmembers, Integer>{
	int deleteByGroupid(Integer groupid);
	
	int deleteByUseridAndGroupid(Long userid, Integer groupid);
	
	List<Groupmembers> findByGroupid(Integer groupid);
	
	Groupmembers findByGroupidAndUserid(Integer groupid,Long userid);

	@Query(value="SELECT g.image,g.id,g.name,m.userid,m.userName from tgroup g LEFT JOIN tgroup_members m ON g.id = m.groupid where groupid in (SELECT groupid from tgroup_members where userid = ?1) and m.userName LIKE CONCAT('%',?2,'%') LIMIT 0,10",nativeQuery=true)
	List<Map<String, Object>> searchByUserName(Long userId,String mohu);

	@Query(value="SELECT image,id,`name` from tgroup where id in (SELECT groupid from tgroup_members where userid = ?1) and name LIKE CONCAT('%',?2,'%') GROUP BY id LIMIT 0,10",nativeQuery=true)
	List<Map<String, Object>> searchByGroupName(Long userId, String mohu);

	@Query(value="SELECT groupid from tgroup_members where userid = ?1 and gtype = ?2",nativeQuery=true)
	List<Map<String,Object>> findByUserIdAndGtype(Long userId, Integer code);
}
